Repository Public
1import { apiError, apiSession, clearSessionCookie, clientCtx, json, loginEvent, revokeSession } from "@/lib/auth";2import { sql } from "@/lib/db";3import { audit } from "@/lib/jobs";4import { handle } from "../../_lib";56export const runtime = "nodejs";7export const dynamic = "force-dynamic";89/** Révoque une de ses propres sessions, désignée par son identifiant public (16 hex). */10export async function DELETE(req: Request, { params }: { params: Promise<{ id: string }> }) {11 return handle(async () => {12 const s = await apiSession(req);13 if (s.error) return s.error;14 const { id } = await params;15 if (!/^[0-9a-f]{16}$/.test(id)) return apiError(404, "not_found", "Session introuvable.");16 const rows = await sql<{ token_hash: string }[]>`SELECT token_hash FROM sessions WHERE user_id = ${s.session.user.id} AND revoked_at IS NULL AND left(token_hash, 16) = ${id}`;17 if (rows.length !== 1) return apiError(404, "not_found", "Session introuvable.");18 await revokeSession(rows[0].token_hash);19 const current = rows[0].token_hash === s.session.tokenHash;20 await loginEvent({ userId: s.session.user.id, kind: "revoke", ctx: clientCtx(req), detail: { what: "session", session: id, current } });21 await audit(s.session.user.id, "session.revoke", "session", id, { current });22 return json({ ok: true, current }, 200, current ? { "set-cookie": clearSessionCookie() } : undefined);23 });24}25